<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加指导教师</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../css/oksub.css">
</head>
<body>
    <div class="ok-body">
        <!--form表单-->
        <form class="layui-form layui-form-pane ok-form">
            必填部分
            <hr class="layui-bg-black">
            <div class="layui-form-item">
                <label class="layui-form-label">辅导员Id</label>
                <div class="layui-input-block">
                    <input type="text" name="instructorid" placeholder="请输入辅导员id（工号）" autocomplete="off" class="layui-input" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">辅导员姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="counselorsname" placeholder="请输入辅导员姓名" autocomplete="off" class="layui-input" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">辅导员密码</label>
                <div class="layui-input-block">
                    <input type="password" name="password" placeholder="请输入辅导员密码" autocomplete="off" class="layui-input" lay-verify="required">
                </div>
            </div>
            选填部分（可空）
            <hr class="layui-bg-black">
            <div class="layui-form-item">
                <label class="layui-form-label">辅导员头像</label>
                <div class="layui-input-block">
                    <input type="text" name="photo" placeholder="请输入辅导员头像路径" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="add">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
    <!--js逻辑-->
    <script src="../../lib/layui/layui.js"></script>
    <script>
        layui.use(["element", "form", "okLayer", "jquery","okMd5"], function () {
            let form = layui.form;
            let okLayer = layui.okLayer;
            let $ = layui.jquery;
            let okMd5 = layui.okMd5;

            // 获取contentText
            const ctx = function () {
                let pathName = document.location.pathname;
                let index = pathName.substr(1).indexOf("/");
                let result = pathName.substr(0, index + 1);
                return result;
            }();
            form.on("submit(add)", function (data) {
                data.field.password=okMd5(data.field.password);
                $.ajax({
                    url:ctx+'/admin/addInstructor',
                    type:'POST',
                    data:data.field,
                    dataType:'json',
                    error:function(){
                        okLayer.redCrossMsg("服务器错误")
                    },
                    success:function(data){
                        if (data.code == 200) {
                            // 业务正常
                            okLayer.greenTickMsg("添加成功", function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        } else {
                            // 业务异常
                            okLayer.yellowSighMsg(data.msg)
                        }
                    }
                });
                return false;
            });
        });
    </script>
</body>
</html>
